package problems.contest;

import utils.beans.ListNode;

/**
 * @author dubulingbo, 2023/8/13 10:55.
 */
public class WCT358 {
    public int maxSum(int[] nums) {
        int n = nums.length;
        int max = -1;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (maxDig(nums[i]) == maxDig(nums[j])) {
                    max = Math.max(max, nums[i] + nums[j]);
                }
            }
        }

        return max;
    }

    private int maxDig(int num) {
        int max = 0;
        while (num > 0) {
            max = Math.max(max, num % 10);
            num /= 10;
        }
        return max;
    }


    public ListNode doubleIt(ListNode head) {
        ListNode newHead = reverseList(head);
        int d = 0;
        head = newHead;

        while (head != null) {
            d += (head.val * 2);
            head.val = d % 10;
            d /= 10;
            head = head.next;
        }

        newHead = reverseList(newHead);

        if (d > 0) {
            newHead = new ListNode(d, newHead);
        }

        return newHead;
    }

    private ListNode reverseList(ListNode head) {
        ListNode ass = null;
        while (head != null) {
            ListNode nxt = head.next;
            head.next = ass;
            ass = head;
            head = nxt;
        }
        return ass;
    }

}
